/***************************************************************************
  A SymbolBrowser plugin for JuffEd editor.

  Copyright: 2010 Aleksander Sokoloff <asokol@mail.ru>


  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License.
  version 2 as published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
***************************************************************************/


#include <QDebug>
#include "symbol.h"
#include <QtAlgorithms>


/**********************************************************
 *
 **********************************************************/
SymbolInfo::SymbolInfo(const SymbolType type, const QString &name)
{
    mType = type;
    mName = QString(name);
    mLine = -1;
}


/**********************************************************
 *
 **********************************************************/
SymbolInfo::SymbolInfo(const SymbolInfo *other)
{
    mType = other->mType;
    mName = other->mName;
    mLine = other->mLine;
    mDetailedText = other->mDetailedText;
}



/**********************************************************
 *
 **********************************************************/
Symbol::Symbol(const SymbolType type, const QString &name, Symbol *parent):
    SymbolInfo(type, name),
    QList<Symbol *>()
{
    mLine = -1;
    mParent = NULL;
    setParent(parent);
    mRelatedSymbol = NULL;
}


/**********************************************************
 *
 **********************************************************/
Symbol::~Symbol()
{
    qDeleteAll(begin(), end());
    delete mRelatedSymbol;
}


/**********************************************************
 *
 **********************************************************/
void Symbol::clear()
{
    qDeleteAll(begin(), end());
    QList<Symbol*>::clear();
}


/**********************************************************
 *
 **********************************************************/
void Symbol::setName(const QString &name)
{
    mName = QString(name);
}


/**********************************************************
 *
 **********************************************************/
void Symbol::setDetailedText(const QString &text)
{
    mDetailedText = QString(text);
}


/**********************************************************
 *
 **********************************************************/
void Symbol::setLine(int line)
{
    mLine = line;
}


/**********************************************************
 *
 **********************************************************/
void Symbol::setSymbolType(SymbolType value)
{
    mType = value;
}


/**********************************************************
 *
 **********************************************************/
void Symbol::setParent(Symbol *parent)
{
    int n;
    if (mParent && (n=mParent->indexOf(this)) > -1 )
        mParent->takeAt(n);

    mParent = parent;

    if (mParent)
        mParent->append(this);
}


/**********************************************************
 *
 **********************************************************/
void Symbol::setRelatedSymbol(Symbol *value)
{
    mRelatedSymbol = value;
}


/**********************************************************
 *
 **********************************************************/
Symbol* Symbol::find(const QString &name) const
{
    int size = this->size();
    for (int i=0; i<size; ++i)
    {
        Symbol *s = at(i);
        if (s->name() == name)
            return s;
    }

    return NULL;
}


/**********************************************************
 *
 **********************************************************/
Symbol* Symbol::find(const QString &name, SymbolType type) const
{
    int size = this->size();
    for (int i=0; i<size; ++i)
    {
        Symbol *s = at(i);
        if ((s->name() == name) && (s->type() == type))
            return s;
    }

    return NULL;
}
